package com.briup.day24;

import java.io.*;

/**
 * @author ThinkBook
 */
public class Test02_Buffer {
    public static void main(String[] args) {
test02();
    }
    public static  void test01(){

    }
    /**
     * 使用缓冲的字符流完成文本文件的拷贝
     * BufferedReader readLine()
     * BufferedWriter newLine()
     */
    public static void test02() {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            // 获取字符缓冲输入流和输出流
            FileReader fr = new FileReader("src/file/name.txt");
            FileWriter fw = new FileWriter("src/file/name02.txt");
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);
            // 完成拷贝
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                // ready()方法，判断文件是否读取到末尾，读取末尾（流没有数据），返回false
                if (br.ready()) {
                    bw.newLine();
                }
            }
            System.out.println("name.txt文件拷贝完成");
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public static void main01(String[] args) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        long start = System.currentTimeMillis();
        long end = 0L;
        try {
            FileInputStream  fis = new FileInputStream("src/file/test01.wmv");
            bis = new BufferedInputStream(fis);
            FileOutputStream fos = new FileOutputStream("src/file/test03.wmv");
            bos = new BufferedOutputStream(fos);
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            end = System.currentTimeMillis();
            System.out.println("一共耗时：" + (end - start) + "毫秒");
        }
    }
}
